package com.ltu.timkor6.gamesite.games.towerofhanoi.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.ltu.timkor6.gamesite.shared.Result;
import com.ltu.timkor6.gamesite.users.client.Users;

public class TowerOfHanoi
{
	//private Users users;
	private TowerOfHanoiComposite composite;
	
	private TowerOfHanoiServiceAsync service = GWT.create(TowerOfHanoiService.class);
	
	public TowerOfHanoi(Users users)
	{
		checkUser();
		composite = new TowerOfHanoiComposite(this);
	}
	
	public void checkSubmit()
	{
		int elements = 0;
		boolean error = false;
		
		try
		{
			elements = Integer.parseInt(this.composite.getNumber());
		}
		catch(NumberFormatException e)
		{
			error = true;
			this.composite.addError("You must enter a number.");
		}
		
		if(error == false)
		{
			if(0 < elements && elements < 10)
			{
				composite.setElements(elements);
				start(elements);
			}
			else
			{
				this.composite.addError("This Hanoi game requires a number between 1 and 9.");
			}
		}
	}
	
	/**
	 * For user authentification
	 */
	public void checkUser()
	{
		// Initialize the service proxy.
		if(service == null)
		{
			service = GWT.create(TowerOfHanoiService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(Boolean signedIn)
			{
			}
		};
		
		// Make the call to the stock price service.
		service.checkUser(callback);
	}
	
	/**
	 * Send start game.
	 *  - Defines number of questions to answer.
	 * @param questions
	 */
	public void start(int elements)
	{
		// Initialize the service proxy.
		if(service == null)
		{
			service = GWT.create(TowerOfHanoiService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<Integer> callback = new AsyncCallback<Integer>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(Integer minMoves)
			{
				composite.setMinMoves(minMoves);
				composite.startGame();
			}
		};
		
		// Make the call to the stock price service.
		service.start(elements, callback);
	}
	
	/**
	 * Send start game.
	 *  - Defines number of questions to answer.
	 * @param questions
	 */
	public void finished(int moves)
	{
		// Initialize the service proxy.
		if(service == null)
		{
			service = GWT.create(TowerOfHanoiService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<Result> callback = new AsyncCallback<Result>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(Result result)
			{
				composite.finished(result);
			}
		};
		
		// Make the call to the stock price service.
		service.finished(moves, callback);
	}
	
	public Composite getComposite()
	{
		return composite;
	}
}
